home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / NEWSOFT / AUGUST / WORKDISC / !Forthmacs / spread / algebra next >
Text File  |  1995-10-25  |  3KB  |  100 lines

  1. \ algebraic functions
  2.  
  3. vocabulary algebra
  4.  
  5. algebra also definitions    \ defined in portability layer
  6.  
  7. create op_stack 20 cells allot    \ operator stack for algebraic
  8.                 \ equation compilation
  9.  
  10. \ col_id function assigns n to id at compile time  ( n --)
  11. \ expects row # on tos at run time.
  12. \ subsequent usage of id fetches double value of cell to stack
  13.  
  14. \ 32-bit
  15. : col_id                \ column_id high-level defining word.
  16.     create    ,            \ creates col ids a-z
  17.     does>    @ spcells cell+ @ ;    \ expect a # on the tos and
  18.                     \ pushes the cell value onto 
  19.                                         \ the parameter stack
  20.                     
  21. : assign_id
  22.     col_max 0            \ loop used to assign values to
  23.     do  i col_id  loop ;        \ the alphabetic columns
  24.  
  25. assign_id    a b c d e f g h i j k l m n o p
  26.                  q r s t u v w x y z
  27.  
  28. \ for example:  1 a  returns the double-int value of cell  1 a
  29. \ column ids A-Z return values of 0-25 respectively
  30.  
  31. : opp@        ( -- addr )    \ return oprnd stack position
  32.     op_stack dup @ + ;    \ 1st location is stack ptr
  33.  
  34. : >op        \ ( cfa prec -- )
  35.     2 cells op_stack +!
  36.     opp@ 2! ;        \ store cfa and precedence top of oprnd stack
  37.                     
  38. : op>
  39.     opp@ 2@    ( cfa prec )
  40.     2 cells negate op_stack +!    \ pop cfa and prec off oprnd
  41.     drop compile, ;            \ stack and compile into dict.
  42.  
  43. : prec?        \ ( -- prec )
  44.     opp@ @ ;        \ return precedence from top of oprnd stack
  45.  
  46. : ]a                \ end algebraic compilation
  47.     begin    prec?        
  48.     while    op>        \ pop remaining oprnds off stk
  49.     repeat            \ and compile then select forth
  50.     forth ; immediate    \ vocabulary again 
  51.  
  52. \ create high-level definition that performs algebraic
  53. \ compilation.  see text for details of operation
  54.  
  55. : infix
  56.     ' create            \ create new algebraic operator
  57.         swap , , immediate    \ compile cfa of forth operator
  58.     does>   2@            \ and assigned precedence
  59.         begin dup prec? > not    \ at compile time execute if
  60.                     \ prec is lower than oprnd on
  61.         while    >r >r  op>  r> r>
  62.         repeat
  63.         >op ;            \ top of oprnd stack
  64.  
  65. 7 infix * *
  66. 7 infix / /
  67. 6 infix + +
  68. 6 infix - -
  69. 5 infix mod mod
  70.  
  71. : )missing                \ missing ) message
  72.     true abort" missing )" ;    \ if missing then abort
  73.  
  74. : (                    \ left paren
  75.     ['] )missing  1 >op ;        \ prec=1 cfa= )missing message
  76.     immediate            \ push on oprnd stack
  77.  
  78. \ Forth needs to be before algebra in the search order
  79.  
  80. only forth spread also algebra also  forth also
  81. algebra definitions
  82.  
  83. : )
  84.     [ forth ]            \ right paren
  85.     begin    1 prec? <        \ causes all items on oprnd
  86.     while    op>            \ stack to be compiled until
  87.     repeat
  88.     1 prec? =            \ left paren found
  89.     if    2 cells negate op_stack +!    \ left paren should have prec.
  90.     else    true abort" missing ("        \ of 1 else error msg output
  91.     then ;  immediate
  92.  
  93. spread definitions
  94.  
  95. : a[                    \ start algebraic compilation
  96.     op_stack off            \ reset oprnd stack and
  97.     algebra ; immediate        \ select algebra vocabulary
  98.